﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using DM_Article.Model;
using DM_Article.Bll;

public partial class admin_admin_adminchangepass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["username"] == null)
            {
                Response.Write("<script language='javascript'>alert('非法操作，请先登录！');location.href = 'admin_login.aspx';</script>");
            }
        }
        label.Text = Session["username"].ToString();
    }

    protected void btn_passwordchange_Click(object sender, EventArgs e)
    {
        using (OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAccess"].ConnectionString))
        {
            cn.Open();
            string oldpassword = this.txtoldpassword.Text.Trim();
            string newpassword = this.txtnewpassword.Text.Trim();
            string confirmnewpassword = this.txtconfirmnewpassword.Text.Trim();
            if (oldpassword == "")
            {
                Response.Write("<script language='javascript'>alert('请填写旧密码');</script>");
                this.txtnewpassword.Text = "";
                this.txtconfirmnewpassword.Text = "";
                this.txtoldpassword.Focus();
            }
            else if (newpassword == "")
            {
                Response.Write("<script language='javascript'>alert('请填写新密码');</script>");
                this.txtconfirmnewpassword.Text = "";
                this.txtnewpassword.Focus();                
            }
            else if (confirmnewpassword == "")
            {
                Response.Write("<script language='javascript'>alert('请确认新密码');</script>");
                this.txtconfirmnewpassword.Focus();                
            }
            else if (newpassword != confirmnewpassword)
            {
                Response.Write("<script language='javascript'>alert('两次输入的密码不一致');</script>");
                this.txtnewpassword.Text = "";
                this.txtconfirmnewpassword.Text = "";
                this.txtnewpassword.Focus();
            }
            else
            {
                admin adn = new admin();
                adn = adminbll.getAdminDetailByName(cn, Session["username"].ToString());
                if (adn.password == oldpassword)
                {
                    adn.password = newpassword;
                    if (adminbll.update(cn, adn) > 0)
                    {
                        Response.Write("<script language='javascript'>alert('修改成功！');location.href = 'admin_server.aspx';</script>");
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('修改失败！请重新修改');</script>");
                        this.txtnewpassword.Text = "";
                        this.txtconfirmnewpassword.Text = "";
                        this.txtoldpassword.Text = "";
                        this.txtoldpassword.Focus();
                    }
                }
            }
        }
    }

    protected void btn_back_Click(object sender, EventArgs e)
    {
        Response.Redirect("admin_server.aspx");
    }
}